package net.forsalemall.android;

import java.util.List;

import org.json.JSONObject;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.IInterface;
import android.util.Log;

import com.google.android.c2dm.C2DMBaseReceiver;
import com.google.android.c2dm.GCMConstants;
import com.google.android.c2dm.GCMUtils;
import com.utils.Utils;

public class C2DMReceiver extends C2DMBaseReceiver {


	public C2DMReceiver() {
		super(GCMConstants.SENDER_ID);
	}

	@Override
	public void onRegistered(Context context, String registrationId)
			throws java.io.IOException {
		GCMUtils.setRegId(context, registrationId);
	};

	@Override
	protected void onMessage(Context context, Intent intent) {

		Bundle extras = intent.getExtras();
		String data = "";
		if (extras != null) {
			data = extras.get("payload").toString();
			handleNotification(Utils.getDecodedString(data));
		}
	}

	@Override
	public void onError(Context context, String errorId) {
		Log.e("C2DM", "Error occured!!!");
	}

	private int initialValue = 101;
	private void handleNotification(String data) {
		try {
			++initialValue;
			Log.i("DREG", "DATA="+data);
			JSONObject jo = new JSONObject(data);
			String action = jo.optString("action");
			String message = jo.optString("text");
			
			if("url".equalsIgnoreCase(action)){
				List<String> urls = Utils.getLRLS(message);
				if(urls.size()>0){
					Intent intent = new Intent("android.intent.action.VIEW",
							Uri.parse(urls.get(0)));
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(intent);
				}
			} else {
				String title = jo.optString("title");			
				String imageUrl = null;
				boolean isShowImage ="show_image".equalsIgnoreCase(action);
				boolean isDialog =  false;
				if(isShowImage){
					 imageUrl = jo.optString("url");
					 isDialog = isShowImage&&(imageUrl!=null && imageUrl.length()>5);
				}

				Intent contentIntent = new Intent(this, PushNotificationActivity.class);
				contentIntent.putExtra(GCMConstants.KEY_GCM_TITLE, title);
				contentIntent.putExtra(GCMConstants.KEY_GCM_MESSAGE, message);
				contentIntent.putExtra(GCMConstants.KEY_GCM_IMAGE_URL, imageUrl);
				contentIntent.putExtra(GCMConstants.KEY_GCM_NOTIFICATION_ID,
						initialValue);
				contentIntent.putExtra(GCMConstants.KEY_GCM_IS_DIALOG, isDialog);
				contentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

				if (isDialog) {
					startActivity(contentIntent);
				} else {
					PendingIntent appIntent = PendingIntent.getActivity(this,
							initialValue, contentIntent,
							PendingIntent.FLAG_UPDATE_CURRENT);
					Notification notification = new Notification(R.drawable.icon,
							null, System.currentTimeMillis());
					notification
							.setLatestEventInfo(this, title, message, appIntent);
					notification.contentIntent = appIntent;
					((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
							.notify(initialValue, notification);
				}
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
